home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / as / sprite / sparc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  27.3 KB  |  1,348 lines

  1. /* sparc.c -- Assemble for the SPARC
  2.    Copyright (C) 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GAS, the GNU Assembler.
  5.  
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GAS is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GAS; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23.  
  24. #include "sparc-opcode.h"
  25. #include "as.h"
  26. #include "frags.h"
  27. #include "struc-symbol.h"
  28. #include "flonum.h"
  29. #include "expr.h"
  30. #include "hash.h"
  31. #include "md.h"
  32. #include "sparc.h"
  33. #include "write.h"
  34. #include "read.h"
  35. #include "symbols.h"
  36.  
  37. #undef NDEBUG
  38.  
  39. void md_begin();
  40. void md_end();
  41. void md_number_to_chars();
  42. void md_assemble();
  43. char *md_atof();
  44. void md_convert_frag();
  45. void md_create_short_jump();
  46. void md_create_long_jump();
  47. int  md_estimate_size_before_relax();
  48. void md_number_to_imm();
  49. void md_number_to_disp();
  50. void md_number_to_field();
  51. void md_ri_to_chars();
  52. void emit_relocations();
  53. static void sparc_ip();
  54.  
  55. relax_typeS md_relax_table[] = { 0 };
  56.  
  57. /* handle of the OPCODE hash table */
  58. static struct hash_control *op_hash = NULL;
  59.  
  60. static void s_seg(), s_proc(), s_data1(), s_reserve(), s_common();
  61. extern void s_globl(), s_long(), s_short(), s_space(), cons();
  62.  
  63. pseudo_typeS
  64. md_pseudo_table[] = {
  65.     { "common",     s_common,   0 },
  66.     { "global",     s_globl,    0 },
  67.     { "half",       cons,       2 },
  68.     { "proc",       s_proc,     0 },
  69.     { "reserve",    s_reserve,  0 },
  70.     { "seg",        s_seg,      0 },
  71.     { "skip",       s_space,    0 },
  72.     { "word",       cons,       4 },
  73.     { NULL,         0,          0 },
  74. };
  75.  
  76. int md_short_jump_size = 4;
  77. int md_long_jump_size = 4;
  78. int omagic  =  (0x103 << 16) | OMAGIC;  /* Magic number for header */
  79.  
  80. /* This array holds the chars that always start a comment.  If the
  81.     pre-processor is disabled, these aren't very useful */
  82. char comment_chars[] = "!";    /* JF removed '|' from comment_chars */
  83.  
  84. /* This array holds the chars that only start a comment at the beginning of
  85.    a line.  If the line seems to have the form '# 123 filename'
  86.    .line and .file directives will appear in the pre-processed output */
  87. /* Note that input_file.c hand checks for '#' at the beginning of the
  88.    first line of the input file.  This is because the compiler outputs
  89.    #NO_APP at the beginning of its output. */
  90. /* Also note that '/*' will always start a comment */
  91. char line_comment_chars[] = "#";
  92.  
  93. /* Chars that can be used to separate mant from exp in floating point nums */
  94. char EXP_CHARS[] = "eE";
  95.  
  96. /* Chars that mean this number is a floating point constant */
  97. /* As in 0f12.456 */
  98. /* or    0d1.2345e12 */
  99. char FLT_CHARS[] = "rRsSfFdDxXpP";
  100.  
  101. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  102.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  103.    but nothing is ideal around here.
  104.  */
  105. int size_reloc_info = sizeof(struct reloc_info_sparc);
  106.  
  107. static unsigned char octal[256];
  108. #define isoctal(c)  octal[c]
  109. static unsigned char toHex[256];
  110.  
  111. /*
  112.  *  anull bit - causes the branch delay slot instructions to not be executed 
  113.  */
  114. #define ANNUL       (1 << 29)
  115.  
  116. struct sparc_it {
  117.     char    *error;
  118.     unsigned long opcode;
  119.     struct nlist *nlistp;
  120.     expressionS exp;
  121.     int pcrel;
  122.     enum reloc_type reloc;
  123. } the_insn, set_insn;
  124.  
  125. #ifdef __STDC__
  126. static void print_insn(struct sparc_it *insn);
  127. static int getExpression(char *str);
  128. #else
  129. static void print_insn();
  130. static int getExpression();
  131. #endif
  132. static char *expr_end;
  133. static int special_case;
  134.  
  135. #define SPECIAL_CASE_SET    1
  136.  
  137. /*
  138.  * sort of like s_lcomm
  139.  *
  140.  */
  141. static void
  142. s_reserve()
  143. {
  144.     char *name;
  145.     char c;
  146.     char *p;
  147.     int temp;
  148.     symbolS *symbolP;
  149.  
  150.     name = input_line_pointer;
  151.     c = get_symbol_end();
  152.     p = input_line_pointer;
  153.     *p = c;
  154.     SKIP_WHITESPACE();
  155.     if ( * input_line_pointer != ',' ) {
  156.     as_warn("Expected comma after name");
  157.     ignore_rest_of_line();
  158.     return;
  159.     }
  160.     input_line_pointer ++;
  161.     if ((temp = get_absolute_expression()) < 0) {
  162.     as_warn("BSS length (%d.) <0! Ignored.", temp);
  163.     ignore_rest_of_line();
  164.     return;
  165.     }
  166.     *p = 0;
  167.     symbolP = symbol_find_or_make(name);
  168.     *p = c;
  169.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  170.     as_warn("bad .reserve segment: `%s'", input_line_pointer);
  171.     return;
  172.     }
  173.     input_line_pointer += 6;
  174.     if (symbolP->sy_other == 0 
  175.         && symbolP->sy_desc  == 0
  176.     && ((symbolP->sy_type  == N_BSS
  177.     && symbolP->sy_value == local_bss_counter)
  178.     || ((symbolP->sy_type & N_TYPE) == N_UNDF
  179.     && symbolP->sy_value == 0))) {
  180.         symbolP->sy_value = local_bss_counter;
  181.         symbolP->sy_type  = N_BSS;
  182.         symbolP->sy_frag  = & bss_address_frag;
  183.         local_bss_counter += temp;
  184.     } else {
  185.     as_warn( "Ignoring attempt to re-define symbol from %d. to %d.",
  186.         symbolP->sy_value, local_bss_counter );
  187.     }
  188.     demand_empty_rest_of_line();
  189.     return;
  190. }
  191.  
  192. static void
  193. s_common()
  194. {
  195.     register char *name;
  196.     register char c;
  197.     register char *p;
  198.     register int temp;
  199.     register symbolS *    symbolP;
  200.  
  201.     name = input_line_pointer;
  202.     c = get_symbol_end();
  203.     /* just after name is now '\0' */
  204.     p = input_line_pointer;
  205.     *p = c;
  206.     SKIP_WHITESPACE();
  207.     if ( * input_line_pointer != ',' ) {
  208.     as_warn("Expected comma after symbol-name");
  209.     ignore_rest_of_line();
  210.     return;
  211.     }
  212.     input_line_pointer ++; /* skip ',' */
  213.     if ( (temp = get_absolute_expression ()) < 0 ) {
  214.     as_warn(".COMMon length (%d.) <0! Ignored.", temp);
  215.     ignore_rest_of_line();
  216.     return;
  217.     }
  218.     *p = 0;
  219.     symbolP = symbol_find_or_make (name);
  220.     *p = c;
  221.     if (   (symbolP->sy_type & N_TYPE) != N_UNDF ||
  222.         symbolP->sy_other != 0 || symbolP->sy_desc != 0) {
  223.     as_warn( "Ignoring attempt to re-define symbol");
  224.     ignore_rest_of_line();
  225.     return;
  226.     }
  227.     if (symbolP->sy_value) {
  228.     if (symbolP->sy_value != temp) {
  229.         as_warn( "Length of .comm \"%s\" is already %d. Not changed to %d.",
  230.         symbolP->sy_name, symbolP->sy_value, temp);
  231.     }
  232.     } else {
  233.     symbolP->sy_value = temp;
  234.     symbolP->sy_type |= N_EXT;
  235.     }
  236.     know(symbolP->sy_frag == &zero_address_frag);
  237.     if (strncmp(input_line_pointer, ",\"bss\"", 6) != 0) {
  238.     p=input_line_pointer;
  239.     while(*p && *p!='\n')
  240.         p++;
  241.     c= *p;
  242.     *p='\0';
  243.     as_warn("bad .common segment: `%s'", input_line_pointer);
  244.     *p=c;
  245.     return;
  246.     }
  247.     input_line_pointer += 6;
  248.     demand_empty_rest_of_line();
  249.     return;
  250. }
  251.  
  252. static void
  253. s_seg()
  254. {
  255.  
  256.     if (strncmp(input_line_pointer, "\"text\"", 6) == 0) {
  257.     input_line_pointer += 6;
  258.     s_text();
  259.     return;
  260.     }
  261.     if (strncmp(input_line_pointer, "\"data\"", 6) == 0) {
  262.     input_line_pointer += 6;
  263.     s_data();
  264.     return;
  265.     }
  266.     if (strncmp(input_line_pointer, "\"data1\"", 7) == 0) {
  267.     input_line_pointer += 7;
  268.     s_data1();
  269.     return;
  270.     }
  271.     as_warn("Unknown segment type");
  272.     demand_empty_rest_of_line();
  273.     return;
  274. }
  275.  
  276. static void
  277. s_data1()
  278. {
  279.     subseg_new(SEG_DATA, 1);
  280.     demand_empty_rest_of_line();
  281.     return;
  282. }
  283.  
  284. static void
  285. s_proc()
  286. {
  287.     extern char is_end_of_line[];
  288.  
  289.     while (!is_end_of_line[*input_line_pointer]) {
  290.     ++input_line_pointer;
  291.     }
  292.     ++input_line_pointer;
  293.     return;
  294. }
  295.  
  296. static void
  297. s_sparc_align()
  298. {
  299.     register unsigned int temp;
  300.     register long int temp_fill;
  301.     unsigned int i;
  302.  
  303.     temp = get_absolute_expression ();
  304. #define MAX_ALIGNMENT (1 << 15)
  305.     if ( temp > MAX_ALIGNMENT ) {
  306.     as_warn("Alignment too large: %d. assumed.", temp = MAX_ALIGNMENT);
  307.     }
  308.  
  309.     /*
  310.      * For the sparc, `.align (1<<n)' actually means `.align n'
  311.      * so we have to convert it.
  312.      */
  313.     if (temp != 0) {
  314.     for (i = 0; (temp & 1) == 0; temp >>= 1, ++i)
  315.         ;
  316.     }
  317.     if (temp != 1) {
  318.     as_warn("Alignment not a power of 2");
  319.     }
  320.     temp = i;
  321.     if (*input_line_pointer == ',') {
  322.     input_line_pointer ++;
  323.     temp_fill = get_absolute_expression ();
  324.     } else {
  325.     temp_fill = 0;
  326.     }
  327.     /* Only make a frag if we HAVE to. . . */
  328.     if (temp && ! need_pass_2) {
  329.     frag_align (temp, (int)temp_fill);
  330.     }
  331.     demand_empty_rest_of_line();
  332.     return;
  333. }
  334.  
  335. /* This function is called once, at assembler startup time.  It should
  336.    set up all the tables, etc. that the MD part of the assembler will need.  */
  337. void
  338. md_begin()
  339. {
  340.   register char *retval = NULL;
  341.   int lose = 0;
  342.   register unsigned int i = 0;
  343.  
  344.   op_hash = hash_new();
  345.   if (op_hash == NULL)
  346.     as_fatal("Virtual memory exhausted");
  347.  
  348.   while (i < NUMOPCODES)
  349.     {
  350.       const char *name = sparc_opcodes[i].name;
  351.       retval = hash_insert(op_hash, name, &sparc_opcodes[i]);
  352.       if(retval != NULL && *retval != '\0')
  353.     {
  354.       fprintf (stderr, "internal error: can't hash `%s': %s\n",
  355.            sparc_opcodes[i].name, retval);
  356.       lose = 1;
  357.     }
  358.       do
  359.     {
  360.       if (sparc_opcodes[i].match & sparc_opcodes[i].lose)
  361.         {
  362.           fprintf (stderr, "internal error: losing opcode: `%s' \"%s\"\n",
  363.                sparc_opcodes[i].name, sparc_opcodes[i].args);
  364.           lose = 1;
  365.         }
  366.       ++i;
  367.     } while (i < NUMOPCODES
  368.          && !strcmp(sparc_opcodes[i].name, name));
  369.     }
  370.  
  371.   if (lose)
  372.     as_fatal ("Broken assembler.  No assembly attempted.");
  373.  
  374.   for (i = '0'; i < '8'; ++i)
  375.     octal[i] = 1;
  376.   for (i = '0'; i <= '9'; ++i)
  377.     toHex[i] = i - '0';
  378.   for (i = 'a'; i <= 'f'; ++i)
  379.     toHex[i] = i + 10 - 'a';
  380.   for (i = 'A'; i <= 'F'; ++i)
  381.     toHex[i] = i + 10 - 'A';
  382. }
  383.  
  384. void
  385. md_end()
  386. {
  387.     return;
  388. }
  389.  
  390. void
  391. md_assemble(str)
  392.     char *str;
  393. {
  394.     char *toP;
  395.     int rsd;
  396.  
  397.     assert(str);
  398.     sparc_ip(str);
  399.  
  400.     switch (special_case) {
  401.  
  402.     case SPECIAL_CASE_SET:
  403.     special_case = 0;
  404.     assert(the_insn.reloc == RELOC_BASE13);
  405.  
  406.     if (the_insn.exp.X_add_symbol != NULL ||
  407.         the_insn.exp.X_subtract_symbol != NULL ||
  408.         ((the_insn.exp.X_add_number & 0xffffe000) !=
  409.           ((the_insn.exp.X_add_number & 0x1000) ? 0xffffe000 : 0))) {
  410.  
  411.         toP = frag_more(4);
  412.         rsd = (the_insn.opcode >> 25) & 0x1f;
  413.         assert(the_insn.opcode == (0x80102000 | (rsd << 25)));
  414.         the_insn.opcode = 0x01000000 | (rsd << 25);
  415.         md_number_to_chars(toP, the_insn.opcode, 4);
  416.             fix_new(
  417.             frag_now,                           /* which frag */
  418.         (toP - frag_now->fr_literal),       /* where */
  419.         4,                                  /* size */
  420.         the_insn.exp.X_add_symbol,
  421.         the_insn.exp.X_subtract_symbol,
  422.         the_insn.exp.X_add_number,
  423.         the_insn.pcrel,
  424.         RELOC_HI22
  425.         );
  426.         if (the_insn.exp.X_add_symbol == NULL &&
  427.                the_insn.exp.X_subtract_symbol == NULL &&
  428.            ((the_insn.exp.X_add_number & 0x000003ff) == 0)) {
  429.             return;
  430.         }
  431.         the_insn.opcode = 0x80102000 | (rsd << 25) | (rsd << 14);
  432.         the_insn.reloc = RELOC_LO10;
  433.         }
  434.     break;
  435.  
  436.     case 0:
  437.     break;
  438.  
  439.     default:
  440.     abort();
  441.     }
  442.  
  443.     toP = frag_more(4);
  444.     /* put out the opcode */
  445.     md_number_to_chars(toP, the_insn.opcode, 4);
  446.  
  447.     /* put out the symbol-dependent stuff */
  448.     if (the_insn.reloc != NO_RELOC) {
  449.     fix_new(
  450.         frag_now,                           /* which frag */
  451.         (toP - frag_now->fr_literal), /* where */
  452.         4,                                  /* size */
  453.         the_insn.exp.X_add_symbol,
  454.         the_insn.exp.X_subtract_symbol,
  455.         the_insn.exp.X_add_number,
  456.         the_insn.pcrel,
  457.         the_insn.reloc
  458.     );
  459.     }
  460. }
  461.  
  462. static void
  463. sparc_ip(str)
  464.     char *str;
  465. {
  466.     char *s;
  467.     const char *args;
  468.     char c;
  469.     unsigned long i;
  470.     struct sparc_opcode *insn;
  471.     char *argsStart;
  472.     unsigned long   opcode;
  473.     unsigned int mask;
  474.     int match = FALSE;
  475.     int comma = 0;
  476.  
  477.     for (s = str; islower(*s) || (*s >= '0' && *s <= '3'); ++s)
  478.     ;
  479.     switch (*s) {
  480.  
  481.     case '\0':
  482.     break;
  483.  
  484.     case ',':
  485.     comma = 1;
  486.  
  487.     /*FALLTHROUGH*/
  488.  
  489.     case ' ':
  490.     *s++ = '\0';
  491.     break;
  492.  
  493.     default:
  494.         as_warn("Unknown opcode: `%s'", str);
  495.         exit(1);
  496.     }
  497.     if ((insn = (struct sparc_opcode *) hash_find(op_hash, str)) == NULL) {
  498.     as_warn("`%s' not in hash table.", str);
  499.     return;
  500.     }
  501.     if (comma) {
  502.     *--s = ',';
  503.     }
  504.     argsStart = s;
  505.     for (;;) {
  506.     opcode = insn->match;
  507.     bzero(&the_insn, sizeof(the_insn));
  508.     the_insn.reloc = NO_RELOC;
  509.  
  510.     /*
  511.      * Build the opcode, checking as we go to make
  512.      * sure that the operands match
  513.      */
  514.     for (args = insn->args; ; ++args) {
  515.         switch (*args) {
  516.  
  517.         case '\0':  /* end of args */
  518.         if (*s == '\0') {
  519.             match = TRUE;
  520.         }
  521.         break;
  522.  
  523.         case '+':
  524.         if (*s == '+') {
  525.             ++s;
  526.             continue;
  527.         }
  528.         if (*s == '-') {
  529.             continue;
  530.         }
  531.         break;
  532.  
  533.         case '[':   /* these must match exactly */
  534.         case ']':
  535.         case ',':
  536.         case ' ':
  537.         if (*s++ == *args)
  538.             continue;
  539.         break;
  540.  
  541.         case '#':   /* must be at least one digit */
  542.         if (isdigit(*s++)) {
  543.             while (isdigit(*s)) {
  544.             ++s;
  545.             }
  546.             continue;
  547.         }
  548.         break;
  549.  
  550.         case 'C':   /* coprocessor state register */
  551.         if (strncmp(s, "%csr", 4) == 0) {
  552.             s += 4;
  553.             continue;
  554.         }
  555.         break;
  556.  
  557.         case 'b':    /* next operand is a coprocessor register */
  558.         case 'c':
  559.         case 'D':
  560.             if (*s++ == '%' && *s++ == 'c' && isdigit(*s)) {
  561.             mask = *s++;
  562.             if (isdigit(*s)) {
  563.             mask = 10 * (mask - '0') + (*s++ - '0');
  564.             if (mask >= 32) {
  565.                 break;
  566.             }
  567.             } else {
  568.             mask -= '0';
  569.             }
  570.             switch (*args) {
  571.  
  572.             case 'b':
  573.             opcode |= mask << 14;
  574.             continue;
  575.  
  576.             case 'c':
  577.             opcode |= mask;
  578.             continue;
  579.  
  580.             case 'D':
  581.             opcode |= mask << 25;
  582.             continue;
  583.             }
  584.         }
  585.         break;
  586.  
  587.         case 'r':   /* next operand must be a register */
  588.         case '1':
  589.         case '2':
  590.         case 'd':
  591.         if (*s++ == '%') {
  592.             switch (c = *s++) {
  593.  
  594.             case 'f':   /* frame pointer */
  595.                 if (*s++ == 'p') {
  596.                 mask = 0x1e;
  597.                 break;
  598.             }
  599.             goto error;
  600.  
  601.             case 'g':   /* global register */
  602.             if (isoctal(c = *s++)) {
  603.                 mask = c - '0';
  604.                 break;
  605.             }
  606.             goto error;
  607.  
  608.             case 'i':   /* in register */
  609.             if (isoctal(c = *s++)) {
  610.                 mask = c - '0' + 24;
  611.                 break;
  612.             }
  613.             goto error;
  614.  
  615.             case 'l':   /* local register */
  616.             if (isoctal(c = *s++)) {
  617.                 mask= (c - '0' + 16) ;
  618.                 break;
  619.             }
  620.             goto error;
  621.  
  622.             case 'o':   /* out register */
  623.             if (isoctal(c = *s++)) {
  624.                 mask= (c - '0' + 8) ;
  625.                 break;
  626.             }
  627.             goto error;
  628.  
  629.             case 's':   /* stack pointer */
  630.                 if (*s++ == 'p') {
  631.                 mask= 0xe;
  632.                 break;
  633.             }
  634.             goto error;
  635.  
  636.             case 'r': /* any register */
  637.                 if (!isdigit(c = *s++)) {
  638.                 goto error;
  639.             }
  640.             /* FALLTHROUGH */
  641.             case '0': case '1': case '2': case '3': case '4':
  642.             case '5': case '6': case '7': case '8': case '9':
  643.             if (isdigit(*s)) {
  644.                 if ((c = 10 * (c - '0') + (*s++ - '0')) >= 32) {
  645.                 goto error;
  646.                 }
  647.             } else {
  648.                 c -= '0';
  649.             }
  650.             mask= c;
  651.             break;
  652.  
  653.             default:
  654.             goto error;
  655.             }
  656.             /*
  657.              * Got the register, now figure out where
  658.              * it goes in the opcode.
  659.              */
  660.             switch (*args) {
  661.  
  662.             case '1':
  663.             opcode |= mask << 14;
  664.             continue;
  665.  
  666.             case '2':
  667.             opcode |= mask;
  668.             continue;
  669.  
  670.             case 'd':
  671.             opcode |= mask << 25;
  672.             continue;
  673.  
  674.             case 'r':
  675.             opcode |= (mask << 25) | (mask << 14);
  676.             continue;
  677.             }
  678.         }
  679.         break;
  680.  
  681.         case 'e':    /* next operand is a floating point register */
  682.         case 'f':
  683.         case 'g':
  684.             if (*s++ == '%' && *s++ == 'f' && isdigit(*s)) {
  685.             mask = *s++;
  686.             if (isdigit(*s)) {
  687.             mask = 10 * (mask - '0') + (*s++ - '0');
  688.             if (mask >= 32) {
  689.                 break;
  690.             }
  691.             } else {
  692.             mask -= '0';
  693.             }
  694.             switch (*args) {
  695.  
  696.             case 'e':
  697.             opcode |= mask << 14;
  698.             continue;
  699.  
  700.             case 'f':
  701.             opcode |= mask;
  702.             continue;
  703.  
  704.             case 'g':
  705.             opcode |= mask << 25;
  706.             continue;
  707.             }
  708.         }
  709.         break;
  710.  
  711.         case 'F':
  712.         if (strncmp(s, "%fsr", 4) == 0) {
  713.             s += 4;
  714.             continue;
  715.         }
  716.         break;
  717.  
  718.         case 'h':       /* high 22 bits */
  719.             the_insn.reloc = RELOC_HI22;
  720.         goto immediate;
  721.  
  722.         case 'l':   /* 22 bit PC relative immediate */
  723.             the_insn.reloc = RELOC_WDISP22;
  724.         the_insn.pcrel = 1;
  725.         goto immediate;
  726.  
  727.         case 'L':   /* 30 bit immediate */
  728.             the_insn.reloc = RELOC_WDISP30;
  729.         the_insn.pcrel = 1;
  730.         goto immediate;
  731.  
  732.         case 'i':   /* 13 bit immediate */
  733.             the_insn.reloc = RELOC_BASE13;
  734.  
  735.         /*FALLTHROUGH*/
  736.  
  737.         immediate:
  738.         if(*s==' ')
  739.           s++;
  740.         if (*s == '%') {
  741.             if ((c = s[1]) == 'h' && s[2] == 'i') {
  742.             the_insn.reloc = RELOC_HI22;
  743.             s+=3;
  744.             } else if (c == 'l' && s[2] == 'o') {
  745.             the_insn.reloc = RELOC_LO10;
  746.             s+=3;
  747.             } else
  748.                 break;
  749.         }
  750.         /* Note that if the getExpression() fails, we will still have
  751.            created U entries in the symbol table for the 'symbols'
  752.            in the input string.  Try not to create U symbols for
  753.            registers, etc. */
  754.         {
  755.             /* This stuff checks to see if the expression ends
  756.                in +%reg If it does, it removes the register from
  757.                the expression, and re-sets 's' to point to the
  758.                right place */
  759.  
  760.             char *s1;
  761.  
  762.             for(s1=s;*s1 && *s1!=','&& *s1!=']';s1++)
  763.                 ;
  764.  
  765.             if(s1!=s && isdigit(s1[-1])) {
  766.                 if(s1[-2]=='%' && s1[-3]=='+') {
  767.                     s1-=3;
  768.                     *s1='\0';
  769.                     (void)getExpression(s);
  770.                     *s1='+';
  771.                     s=s1;
  772.                     continue;
  773.                 } else if(index("goli0123456789",s1[-2]) && s1[-3]=='%' && s1[-4]=='+') {
  774.                     s1-=4;
  775.                     *s1='\0';
  776.                     (void)getExpression(s);
  777.                     *s1='+';
  778.                     s=s1;
  779.                     continue;
  780.                 }
  781.             }
  782.         }
  783.         (void)getExpression(s);
  784.         s = expr_end;
  785.         continue;
  786.  
  787.         case 'a':
  788.         if (*s++ == 'a') {
  789.             opcode |= ANNUL;
  790.             continue;
  791.         }
  792.         break;
  793.  
  794.         case 'A':       /* alternate space */
  795.         if (isdigit(*s)) {
  796.             long num = 0;
  797.  
  798.             if (s[0] == '0' && s[1] == 'x') {
  799.             s += 2;
  800.             while (isxdigit(*s)) {
  801.                 if (isdigit(*s)) {
  802.                 num = num*16 + *s - '0';
  803.                 } else if (isupper(*s)) {
  804.                 num = num*16 + *s - 'A' + 10;
  805.                 } else {
  806.                 num = num*16 + *s - 'a' + 10;
  807.                 }
  808.                 ++s;
  809.             }
  810.             } else {
  811.             while (isdigit(*s)) {
  812.                 num= num*10 + *s-'0';
  813.                 ++s;
  814.             }
  815.             }
  816.             opcode |= num<<5;
  817.             continue;
  818.         }
  819.         break;
  820.         /* abort(); */
  821.  
  822.         case 'p':
  823.         if (strncmp(s, "%psr", 4) == 0) {
  824.             s += 4;
  825.             continue;
  826.         }
  827.         break;
  828.  
  829.         case 'q':   /* floating point queue */
  830.         if (strncmp(s, "%fq", 3) == 0) {
  831.             s += 3;
  832.             continue;
  833.         }
  834.         break;
  835.  
  836.         case 'Q':   /* coprocessor queue */
  837.         if (strncmp(s, "%cq", 3) == 0) {
  838.             s += 3;
  839.             continue;
  840.         }
  841.         break;
  842.  
  843.         case 'S':
  844.         if (strcmp(str, "set") == 0) {
  845.             special_case = SPECIAL_CASE_SET;
  846.             continue;
  847.         }
  848.         break;
  849.  
  850.         case 't':
  851.         if (strncmp(s, "%tbr", 4) != 0)
  852.             break;
  853.         s += 4;
  854.         continue;
  855.  
  856.         case 'w':
  857.         if (strncmp(s, "%wim", 4) != 0)
  858.             break;
  859.         s += 4;
  860.         continue;
  861.  
  862.         case 'y':
  863.         if (strncmp(s, "%y", 2) != 0)
  864.             break;
  865.         s += 2;
  866.         continue;
  867.  
  868.         default:
  869.         abort();
  870.         }
  871.         break;
  872.     }
  873.     error:
  874.     if (match == FALSE)
  875.       {
  876.         /* Args don't match.  */
  877.         if (&insn[1] - sparc_opcodes < NUMOPCODES
  878.         && !strcmp(insn->name, insn[1].name))
  879.           {
  880.         ++insn;
  881.         s = argsStart;
  882.         continue;
  883.           }
  884.         else
  885.           {
  886.         as_warn("Illegal operands");
  887.         return;
  888.           }
  889.       }
  890.     break;
  891.     }
  892.  
  893.     the_insn.opcode = opcode;
  894.     return;
  895. }
  896.  
  897. static int
  898. getExpression(str)
  899.     char *str;
  900. {
  901.     char *save_in;
  902.     segT seg;
  903.  
  904.     save_in = input_line_pointer;
  905.     input_line_pointer = str;
  906.     switch (seg = expression(&the_insn.exp)) {
  907.  
  908.     case SEG_ABSOLUTE:
  909.     case SEG_TEXT:
  910.     case SEG_DATA:
  911.     case SEG_BSS:
  912.     case SEG_UNKNOWN:
  913.     case SEG_DIFFERENCE:
  914.     case SEG_BIG:
  915.     case SEG_NONE:
  916.     break;
  917.  
  918.     default:
  919.     the_insn.error = "bad segment";
  920.     expr_end = input_line_pointer;
  921.     input_line_pointer=save_in;
  922.     return 1;
  923.     }
  924.     expr_end = input_line_pointer;
  925.     input_line_pointer = save_in;
  926.     return 0;
  927. }
  928.  
  929.  
  930. #define MAX_LITTLENUMS 6
  931.  
  932. /*
  933.     This is identical to the md_atof in m68k.c.  I think this is right,
  934.     but I'm not sure.
  935.  
  936.    Turn a string in input_line_pointer into a floating point constant of type
  937.    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
  938.    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
  939.  */
  940. char *
  941. md_atof(type,litP,sizeP)
  942.     char type;
  943.     char *litP;
  944.     int *sizeP;
  945. {
  946.     int    prec;
  947.     LITTLENUM_TYPE words[MAX_LITTLENUMS];
  948.     LITTLENUM_TYPE *wordP;
  949.     char    *t;
  950.     char    *atof_m68k();
  951.  
  952.     switch(type) {
  953.  
  954.     case 'f':
  955.     case 'F':
  956.     case 's':
  957.     case 'S':
  958.     prec = 2;
  959.     break;
  960.  
  961.     case 'd':
  962.     case 'D':
  963.     case 'r':
  964.     case 'R':
  965.     prec = 4;
  966.     break;
  967.  
  968.     case 'x':
  969.     case 'X':
  970.     prec = 6;
  971.     break;
  972.  
  973.     case 'p':
  974.     case 'P':
  975.     prec = 6;
  976.     break;
  977.  
  978.     default:
  979.     *sizeP=0;
  980.     return "Bad call to MD_ATOF()";
  981.     }
  982.     t=atof_m68k(input_line_pointer,type,words);
  983.     if(t)
  984.     input_line_pointer=t;
  985.     *sizeP=prec * sizeof(LITTLENUM_TYPE);
  986.     for(wordP=words;prec--;) {
  987.     md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
  988.     litP+=sizeof(LITTLENUM_TYPE);
  989.     }
  990.     return "";    /* Someone should teach Dean about null pointers */
  991. }
  992.  
  993. /*
  994.  * Write out big-endian.
  995.  */
  996. void
  997. md_number_to_chars(buf,val,n)
  998.     char *buf;
  999.     long val;
  1000.     int n;
  1001. {
  1002.  
  1003.     switch(n) {
  1004.  
  1005.     case 4:
  1006.     *buf++ = val >> 24;
  1007.     *buf++ = val >> 16;
  1008.     case 2:
  1009.     *buf++ = val >> 8;
  1010.     case 1:
  1011.     *buf = val;
  1012.     break;
  1013.  
  1014.     default:
  1015.     abort();
  1016.     }
  1017.     return;
  1018. }
  1019.  
  1020. void
  1021. md_number_to_imm(buf,val,n, fixP, seg_type)
  1022.     char *buf;
  1023.     long val;
  1024.     int n;
  1025.     fixS *fixP;
  1026.     int seg_type;
  1027. {
  1028.     if (seg_type != N_TEXT || fixP->fx_r_type == (int) NO_RELOC) {
  1029.     switch (n) {
  1030.     case 1:
  1031.         *buf = val;
  1032.         break;
  1033.     case 2:
  1034.         *buf++ = (val>>8);
  1035.         *buf = val;
  1036.         break;
  1037.     case 4:
  1038.         *buf++ = (val>>24);
  1039.         *buf++ = (val>>16);
  1040.         *buf++ = (val>>8);
  1041.         *buf = val;
  1042.         break;
  1043.     default:
  1044.         abort();
  1045.     }
  1046.     return;
  1047.     }
  1048.  
  1049.     assert(n == 4);
  1050.     assert(fixP->fx_r_type < NO_RELOC);
  1051.  
  1052.     /*
  1053.      * This is a hack.  There should be a better way to
  1054.      * handle this.
  1055.      */
  1056.     if (fixP->fx_r_type == (int) RELOC_WDISP30 && fixP->fx_addsy) {
  1057.         val += fixP->fx_where + fixP->fx_frag->fr_address;
  1058.     }
  1059.  
  1060.     switch (fixP->fx_r_type) {
  1061.  
  1062.     case RELOC_32:
  1063.     buf[0] = val >> 24;
  1064.     buf[1] = val >> 16;
  1065.     buf[2] = val >> 8;
  1066.     buf[3] = val;
  1067.     break;
  1068.  
  1069. #if 0
  1070.     case RELOC_8:         /* These don't seem to ever be needed. */
  1071.     case RELOC_16:
  1072.     case RELOC_DISP8:
  1073.     case RELOC_DISP16:
  1074.     case RELOC_DISP32:
  1075. #endif
  1076.     case RELOC_WDISP30:
  1077.     val = (val >>= 2) + 1;
  1078.     buf[0] |= (val >> 24) & 0x3f;
  1079.     buf[1]= (val >> 16);
  1080.     buf[2] = val >> 8;
  1081.     buf[3] = val;
  1082.     break;
  1083.  
  1084.     case RELOC_HI22:
  1085.     if(!fixP->fx_addsy) {
  1086.       buf[1] |= (val >> 26) & 0x3f;
  1087.       buf[2] = val >> 18;
  1088.       buf[3] = val >> 10;
  1089.     } else {
  1090.       buf[2]=0;
  1091.       buf[3]=0;
  1092.     }
  1093.     break;
  1094. #if 0
  1095.     case RELOC_22:
  1096.     case RELOC_13:
  1097. #endif
  1098.     case RELOC_LO10:
  1099.     if(!fixP->fx_addsy) {
  1100.       buf[2] |= (val >> 8) & 0x03;
  1101.       buf[3] = val;
  1102.     } else
  1103.       buf[3]=0;
  1104.     break;
  1105. #if 0
  1106.     case RELOC_SFA_BASE:
  1107.     case RELOC_SFA_OFF13:
  1108.     case RELOC_BASE10:
  1109. #endif
  1110.     case RELOC_BASE13:
  1111.     buf[2] |= (val >> 8) & 0x1f;
  1112.     buf[3] = val;
  1113.     break;
  1114.  
  1115.     case RELOC_WDISP22:
  1116.     val = (val >>= 2) + 1;
  1117.     /* FALLTHROUGH */
  1118.     case RELOC_BASE22:
  1119.     buf[1] |= (val >> 16) & 0x3f;
  1120.     buf[2] = val >> 8;
  1121.     buf[3] = val;
  1122.     break;
  1123.  
  1124. #if 0
  1125.     case RELOC_PC10: 
  1126.     case RELOC_PC22: 
  1127.     case RELOC_JMP_TBL:
  1128.     case RELOC_SEGOFF16:
  1129.     case RELOC_GLOB_DAT:
  1130.     case RELOC_JMP_SLOT: 
  1131.     case RELOC_RELATIVE:
  1132. #endif
  1133.  
  1134.     case NO_RELOC:
  1135.     default:
  1136.     as_warn("bad relocation type: 0x%02x", fixP->fx_r_type);
  1137.     break;
  1138.     }
  1139.     return;
  1140. }
  1141.  
  1142. /* should never be called for sparc */
  1143. void
  1144. md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1145.     char *ptr;
  1146.     long from_addr, to_addr;
  1147. {
  1148.     fprintf(stderr, "sparc_create_short_jmp\n");
  1149.     abort();
  1150. }
  1151.  
  1152. /* should never be called for sparc */
  1153. void
  1154. md_number_to_disp(buf,val,n)
  1155.     char    *buf;
  1156.     long    val;
  1157. {
  1158.     fprintf(stderr, "md_number_to_disp\n");
  1159.     abort();
  1160. }
  1161.  
  1162. /* should never be called for sparc */
  1163. void
  1164. md_number_to_field(buf,val,fix)
  1165.     char *buf;
  1166.     long val;
  1167. #ifdef __STDC__
  1168.     void *fix;
  1169. #else
  1170.     char *fix;
  1171. #endif
  1172. {
  1173.     fprintf(stderr, "sparc_number_to_field\n");
  1174.     abort();
  1175. }
  1176.  
  1177. void
  1178. md_ri_to_chars(ri_p, ri)
  1179.     struct reloc_info_sparc *ri_p, ri;
  1180. {
  1181.     unsigned long n;
  1182.     char *p;
  1183.  
  1184.     p = (char *) ri_p;
  1185.     n = ri_p->r_address;
  1186.     p[3] = n;
  1187.     p[2] = n >> 8;
  1188.     p[1] = n >> 16;
  1189.     p[0] = n >> 24;
  1190.     n = (ri_p->r_index << 8) + (ri_p->r_extern << 7) + ((int) ri_p->r_type);
  1191.     p[7] = n;
  1192.     p[6] = n >> 8;
  1193.     p[5] = n >> 16;
  1194.     p[4] = n >> 24;
  1195.     n = ri_p->r_addend;
  1196.     p[11] = n;
  1197.     p[10] = n >> 8;
  1198.     p[9] = n >> 16;
  1199.     p[8] = n >> 24;
  1200.     return;
  1201. }
  1202.  
  1203. /* should never be called for sparc */
  1204. void
  1205. md_convert_frag(fragP)
  1206.     register fragS *fragP;
  1207. {
  1208.     fprintf(stderr, "sparc_convert_frag\n");
  1209.     abort();
  1210. }
  1211.  
  1212. /* should never be called for sparc */
  1213. void
  1214. md_create_long_jump(ptr, from_addr, to_addr, frag, to_symbol)
  1215.     char    *ptr;
  1216.     long    from_addr,
  1217.             to_addr;
  1218.     fragS    *frag;
  1219.     symbolS    *to_symbol;
  1220. {
  1221.     fprintf(stderr, "sparc_create_long_jump\n");
  1222.     abort();
  1223. }
  1224.  
  1225. /* should never be called for sparc */
  1226. int
  1227. md_estimate_size_before_relax(fragP, segtype)
  1228.     register fragS *fragP;
  1229. {
  1230.     fprintf(stderr, "sparc_estimate_size_before_relax\n");
  1231.     abort();
  1232.     return 0;
  1233. }
  1234.  
  1235. #if 0
  1236. /* for debugging only */
  1237. static void
  1238. print_insn(insn)
  1239.     struct sparc_it *insn;
  1240. {
  1241.     char *Reloc[] = {
  1242.     "RELOC_8",
  1243.     "RELOC_16",
  1244.     "RELOC_32",
  1245.     "RELOC_DISP8",
  1246.     "RELOC_DISP16",
  1247.     "RELOC_DISP32",
  1248.     "RELOC_WDISP30",
  1249.     "RELOC_WDISP22",
  1250.     "RELOC_HI22",
  1251.     "RELOC_22",
  1252.     "RELOC_13",
  1253.     "RELOC_LO10",
  1254.     "RELOC_SFA_BASE",
  1255.     "RELOC_SFA_OFF13",
  1256.     "RELOC_BASE10",
  1257.     "RELOC_BASE13",
  1258.     "RELOC_BASE22",
  1259.     "RELOC_PC10",
  1260.     "RELOC_PC22",
  1261.     "RELOC_JMP_TBL",
  1262.     "RELOC_SEGOFF16",
  1263.     "RELOC_GLOB_DAT",
  1264.     "RELOC_JMP_SLOT",
  1265.     "RELOC_RELATIVE",
  1266.     "NO_RELOC"
  1267.     };
  1268.  
  1269.     if (insn->error) {
  1270.     fprintf(stderr, "ERROR: %s\n");
  1271.     }
  1272.     fprintf(stderr, "opcode=0x%08x\n", insn->opcode);
  1273.     fprintf(stderr, "reloc = %s\n", Reloc[insn->reloc]);
  1274.     fprintf(stderr, "exp =  {\n");
  1275.     fprintf(stderr, "\t\tX_add_symbol = %s\n",
  1276.     insn->exp.X_add_symbol ?
  1277.     (insn->exp.X_add_symbol->sy_name ? 
  1278.     insn->exp.X_add_symbol->sy_name : "???") : "0");
  1279.     fprintf(stderr, "\t\tX_sub_symbol = %s\n",
  1280.     insn->exp.X_subtract_symbol ?
  1281.         (insn->exp.X_subtract_symbol->sy_name ? 
  1282.             insn->exp.X_subtract_symbol->sy_name : "???") : "0");
  1283.     fprintf(stderr, "\t\tX_add_number = %d\n",
  1284.     insn->exp.X_add_number);
  1285.     fprintf(stderr, "}\n");
  1286.     return;
  1287. }
  1288. #endif
  1289.  
  1290. /*
  1291.  * Sparc relocations are completely different, so it needs
  1292.  * this machine dependent routine to emit them.
  1293.  */
  1294. void
  1295. emit_relocations(fixP, segment_address_in_file)
  1296.     register fixS *fixP;
  1297.     relax_addressT segment_address_in_file;
  1298. {
  1299.     struct reloc_info_sparc ri;
  1300.     register symbolS *symbolP;
  1301.     extern char *next_object_file_charP;
  1302.     long add_number;
  1303.  
  1304.     bzero((char *) &ri, sizeof(ri));
  1305.     for (; fixP; fixP = fixP->fx_next) {
  1306.  
  1307.     if (fixP->fx_r_type >= (int) NO_RELOC) {
  1308.         fprintf(stderr, "fixP->fx_r_type = %d\n", fixP->fx_r_type);
  1309.         abort();
  1310.     }
  1311.  
  1312.     if ((symbolP = fixP->fx_addsy) != NULL) {
  1313.         ri.r_address = fixP->fx_frag->fr_address +
  1314.             fixP->fx_where - segment_address_in_file;
  1315.         if ((symbolP->sy_type & N_TYPE) == N_UNDF) {
  1316.         ri.r_extern = 1;
  1317.         ri.r_index = symbolP->sy_number;
  1318.         } else {
  1319.         ri.r_extern = 0;
  1320.         ri.r_index = symbolP->sy_type & N_TYPE;
  1321.         }
  1322.         if (symbolP && symbolP->sy_frag) {
  1323.         ri.r_addend = symbolP->sy_frag->fr_address;
  1324.         }
  1325.         ri.r_type = (enum reloc_type) fixP->fx_r_type;
  1326.         if (fixP->fx_pcrel) {
  1327. /*        ri.r_addend -= fixP->fx_where;          */
  1328.         ri.r_addend -= ri.r_address;            
  1329.         } else {
  1330.         ri.r_addend = fixP->fx_addnumber;
  1331.         }
  1332.         md_ri_to_chars((char *) &ri, ri);
  1333.         append(&next_object_file_charP, (char *)& ri, sizeof(ri));
  1334.     }
  1335.     }
  1336.     return;
  1337. }
  1338.  
  1339. int
  1340. md_parse_option(argP,cntP,vecP)
  1341.     char **argP;
  1342.     int *cntP;
  1343.     char ***vecP;
  1344. {
  1345.     return 1;
  1346. }
  1347.  
  1348.